
package com.hlkj.pay.app.devices.impl;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import cn.hutool.core.bean.BeanUtil;
import org.springframework.stereotype.Service;

import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.framework.common.util.json.JsonUtils;
import com.hlkj.pay.app.devices.IMerchantQrTemplateAppService;
import com.hlkj.pay.common.CommonResultCode;
import com.hlkj.pay.infrastructure.model.devices.MerchantQrTemplateDO;
import com.hlkj.pay.service.devices.IMerchantQrTemplateService;
import com.hlkj.pay.vo.devices.req.MerchantQrTemplateReq;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author HlpayTeam
 * @date 2025/01/20 11:11
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class MerchantQrTemplateAppService implements IMerchantQrTemplateAppService {

    private final IMerchantQrTemplateService merchantQrTemplateService;

    @Override
    public CommonResult<Void> saveTemplate(MerchantQrTemplateReq merchantQrTemplateReq) {
        log.info("saveTemplate merchantQrTemplateReq:{}", JsonUtils.toJsonString(merchantQrTemplateReq));
        try {
            MerchantQrTemplateDO merchantQrTemplateDO = BeanUtil.copyProperties(merchantQrTemplateReq, MerchantQrTemplateDO.class);
            if (merchantQrTemplateReq.getId() == null) {
                merchantQrTemplateService.insert(merchantQrTemplateDO);
            }
            else {
                MerchantQrTemplateDO merchantQrTemplate = merchantQrTemplateService.queryDetail(merchantQrTemplateReq.getId());
                if (merchantQrTemplate == null) {
                    return CommonResult.error(CommonResultCode.DATA_NOT_EXIST);
                }
                merchantQrTemplateDO.setId(merchantQrTemplate.getId());
                merchantQrTemplateService.update(merchantQrTemplateDO);
            }
            return CommonResult.success();
        }
        catch (Exception e) {
            e.printStackTrace();
            log.error("saveTemplate error:{}", e, e.getMessage());
            return CommonResult.error(CommonResultCode.SERVICE_ERROR);
        }
    }
}
